home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / nevow / util.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-03-23  |  9.5 KB  |  238 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import inspect
  5. import os.path as os
  6.  
  7. class UnexposedMethodError(Exception):
  8.     '''
  9.     Raised on any attempt to get a method which has not been exposed.
  10.     '''
  11.     pass
  12.  
  13.  
  14. class Expose(object):
  15.     '''
  16.     Helper for exposing methods for various uses using a simple decorator-style
  17.     callable.
  18.  
  19.     Instances of this class can be called with one or more functions as
  20.     positional arguments.  The names of these functions will be added to a list
  21.     on the class object of which they are methods.
  22.  
  23.     @ivar attributeName: The attribute with which exposed methods will be
  24.     tracked.
  25.     '''
  26.     
  27.     def __init__(self, doc = None):
  28.         self.doc = doc
  29.  
  30.     
  31.     def __call__(self, *funcObjs):
  32.         """
  33.         Add one or more functions to the set of exposed functions.
  34.  
  35.         This is a way to declare something about a class definition, similar to
  36.         L{zope.interface.implements}.  Use it like this::
  37.  
  38.         | magic = Expose('perform extra magic')
  39.         | class Foo(Bar):
  40.         |     def twiddle(self, x, y):
  41.         |         ...
  42.         |     def frob(self, a, b):
  43.         |         ...
  44.         |     magic(twiddle, frob)
  45.  
  46.         Later you can query the object::
  47.  
  48.         | aFoo = Foo()
  49.         | magic.get(aFoo, 'twiddle')(x=1, y=2)
  50.  
  51.         The call to C{get} will fail if the name it is given has not been
  52.         exposed using C{magic}.
  53.  
  54.         @param funcObjs: One or more function objects which will be exposed to
  55.         the client.
  56.  
  57.         @return: The first of C{funcObjs}.
  58.         """
  59.         if not funcObjs:
  60.             raise TypeError('expose() takes at least 1 argument (0 given)')
  61.         funcObjs
  62.         for fObj in funcObjs:
  63.             fObj.exposedThrough = getattr(fObj, 'exposedThrough', [])
  64.             fObj.exposedThrough.append(self)
  65.         
  66.         return funcObjs[0]
  67.  
  68.     
  69.     def exposedMethodNames(self, instance):
  70.         '''
  71.         Return an iterator of the names of the methods which are exposed on the
  72.         given instance.
  73.         '''
  74.         for k, callable in inspect.getmembers(instance, inspect.isroutine):
  75.             if self in getattr(callable, 'exposedThrough', []):
  76.                 yield k
  77.                 continue
  78.         
  79.  
  80.     _nodefault = object()
  81.     
  82.     def get(self, instance, methodName, default = _nodefault):
  83.         '''
  84.         Retrieve an exposed method with the given name from the given instance.
  85.  
  86.         @raise UnexposedMethodError: Raised if C{default} is not specified and
  87.         there is no exposed method with the given name.
  88.  
  89.         @return: A callable object for the named method assigned to the given
  90.         instance.
  91.         '''
  92.         method = getattr(instance, methodName, None)
  93.         exposedThrough = getattr(method, 'exposedThrough', [])
  94.         if self not in getattr(method, 'exposedThrough', []):
  95.             if default is self._nodefault:
  96.                 raise UnexposedMethodError(self, methodName)
  97.             default is self._nodefault
  98.             return default
  99.         return method
  100.  
  101.  
  102.  
  103. def escapeToXML(text, isattrib = False):
  104.     '''Borrowed from twisted.xish.domish
  105.  
  106.     Escape text to proper XML form, per section 2.3 in the XML specification.
  107.  
  108.      @type text: L{str}
  109.      @param text: Text to escape
  110.  
  111.      @type isattrib: L{bool}
  112.      @param isattrib: Triggers escaping of characters necessary for use as attribute values
  113.     '''
  114.     text = text.replace('&', '&')
  115.     text = text.replace('<', '<')
  116.     text = text.replace('>', '>')
  117.     if isattrib:
  118.         text = text.replace("'", ''')
  119.         text = text.replace('"', '"')
  120.     
  121.     return text
  122.  
  123.  
  124. def getPOSTCharset(ctx):
  125.     '''Locate the unicode encoding of the POST\'ed form data.
  126.  
  127.     To work reliably you must do the following:
  128.  
  129.       - set the form\'s enctype attribute to \'multipart/form-data\'
  130.       - set the form\'s accept-charset attribute, probably to \'utf-8\'
  131.       - add a hidden form field called \'_charset_\'
  132.  
  133.     For instance::
  134.  
  135.       <form action="foo" method="post" enctype="multipart/form-data" accept-charset="utf-8">
  136.         <input type="hidden" name="_charset_" />
  137.         ...
  138.       </form>
  139.     '''
  140.     inevow = inevow
  141.     import nevow
  142.     request = inevow.IRequest(ctx)
  143.     charset = request.args.get('_charset_', [
  144.         None])[0]
  145.     if charset:
  146.         return charset
  147.     contentType = request.received_headers.get('content-type')
  148.     return 'utf-8'
  149.  
  150. from twisted.python.reflect import qual, namedAny, allYourBase, accumulateBases
  151. from twisted.python.util import uniquify
  152. from twisted.internet.defer import Deferred, succeed, maybeDeferred, DeferredList
  153. from twisted.python import failure
  154. from twisted.python.failure import Failure
  155. from twisted.python import log
  156.  
  157. def remainingSegmentsFactory(ctx):
  158.     return tuple(ctx.tag.postpath)
  159.  
  160.  
  161. def currentSegmentsFactory(ctx):
  162.     return tuple(ctx.tag.prepath)
  163.  
  164.  
  165. class _RandomClazz(object):
  166.     pass
  167.  
  168.  
  169. class _NamedAnyError(Exception):
  170.     '''Internal error for when importing fails.'''
  171.     pass
  172.  
  173.  
  174. def _namedAnyWithBuiltinTranslation(name):
  175.     if name == '__builtin__.function':
  176.         name = 'types.FunctionType'
  177.     elif name == '__builtin__.method':
  178.         return _RandomClazz
  179.     if name == '__builtin__.instancemethod':
  180.         name = 'types.MethodType'
  181.     elif name == '__builtin__.NoneType':
  182.         name = 'types.NoneType'
  183.     elif name == '__builtin__.generator':
  184.         name = 'types.GeneratorType'
  185.     
  186.     return namedAny(name)
  187.  
  188.  
  189. try:
  190.     from pkg_resources import resource_filename
  191. except ImportError:
  192.     
  193.     def resource_filename(modulename, resource_name):
  194.         modulepath = namedAny(modulename).__file__
  195.         return os.path.join(os.path.dirname(os.path.abspath(modulepath)), resource_name)
  196.  
  197.  
  198.  
  199. class CachedFile(object):
  200.     '''
  201.     Helper for caching operations on files in the filesystem.
  202.     '''
  203.     
  204.     def __init__(self, path, loader):
  205.         '''
  206.         @type path: L{str}
  207.         @param path: The path to the associated file in the filesystem.
  208.  
  209.         @param loader: A callable that returns the relevant data; invoked when
  210.         the cache is empty or stale.
  211.         '''
  212.         self.path = path
  213.         self.loader = loader
  214.         self.invalidate()
  215.  
  216.     
  217.     def invalidate(self):
  218.         '''
  219.         Invalidate the cache, forcing a reload from disk at the next attempted
  220.         load.
  221.         '''
  222.         self._mtime = None
  223.  
  224.     
  225.     def load(self, *args, **kwargs):
  226.         '''
  227.         Load this file. Any positional or keyword arguments will be passed
  228.         along to the loader callable, after the path itself.
  229.         '''
  230.         currentTime = os.path.getmtime(self.path)
  231.         if self._mtime is None or currentTime != self._mtime:
  232.             self._cachedObj = self.loader(self.path, *args, **kwargs)
  233.             self._mtime = currentTime
  234.         
  235.         return self._cachedObj
  236.  
  237.  
  238.